package com.kele.config;

import com.kele.realm.UserRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Version 2019
 * @Author:kele
 * @Date:2020/10/29
 * @Content:
 * @Description：
 */
@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){

        ShiroFilterFactoryBean shiroFilterFactoryBean=new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        /**
         * 添加shiro内置的过滤器
         */
        Map<String,String> map=new LinkedHashMap<>();

        //添加访问这些页面需要的权限
        map.put("/add","perms[user:add]");
        map.put("/update","perms[user:update]");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);

        //没有权限设置登陆
        shiroFilterFactoryBean.setLoginUrl("/toLogin");

        //没有某个权限设置跳转页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauth");


        return shiroFilterFactoryBean;
    }



    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager =new DefaultWebSecurityManager();

        securityManager.setRealm(userRealm);
        return securityManager;
    }

    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }
}
